package jdbc;

import com.mysql.jdbc.Driver;

import java.sql.*;
import java.util.Properties;

public class JDBCConnection {
    public static void main(String[] args) throws SQLException {
        //注册驱动
        //这个driver 就是来自mysql-connector-java-5.1.47-bin.jar包中的Driver类
        Driver driver = new Driver();
//        Properties properties = new Properties();
//        properties.setProperty("user","root");
//        properties.setProperty("password","123321");
//        String url="jdbc:mysql://localhost:3306/db01";
//
//        Connection connect = driver.connect(url, properties);
//        System.out.println(connect);
        DriverManager.registerDriver(driver);
        //获取连接
        String url = "jdbc:mysql://localhost:3306/db01";
        String user = "root";
        String password = "123321";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        String sql="SELECT *FROM actor";
        Statement statement = connection.createStatement();
        String sql1="INSERT INTO actor VALUES(5,'张飞','男','1990-11-11','1234567890')";
        PreparedStatement preparedStatement = connection.prepareStatement(sql1);
        int i = preparedStatement.executeUpdate();
        System.out.println(i);
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            Integer id= resultSet.getInt(1);
            String name = resultSet.getString(2);
            String sex= resultSet.getString(3);
            String borndate= resultSet.getString(4);
            String phone= resultSet.getString(5);
            System.out.println(id+" "+name+" "+sex+" "+borndate+" "+phone);
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}

